﻿using System;
using System.Collections.Generic;
using System.Text;
using KPIS.DBM;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_IcmMasObjectType
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();

        public IList<M_IcmMasObjectType> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_IcmMasObjectType> infoList = new List<M_IcmMasObjectType>();
            try
            {
                dbManager.Open();
                strSQL =  " SELECT icm_obj_type_seq,icm_obj_type_code,icm_obj_type_name,record_status "
                         +" ,created_by,created_when,updated_by,updated_when "
                         +" FROM icm_mas_object_type "
                         +" WHERE record_status <> 'D'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_IcmMasObjectType info = new M_IcmMasObjectType();
                        info.icm_obj_type_seq = Convert.ToInt32(drc[i]["icm_obj_type_seq"]);
                        info.icm_obj_type_code = drc[i]["icm_obj_type_code"].ToString();
                        info.icm_obj_type_name = drc[i]["icm_obj_type_name"].ToString();
                        info.record_status = drc[i]["record_status"].ToString();
                        info.created_by = Convert.ToInt32(drc[i]["created_by"]);
                        info.created_when = Convert.ToDateTime(drc[i]["created_when"].ToString());
                        info.updated_by = cn.ConvertNullInt(drc[i]["updated_by"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["updated_when"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<M_IcmMasObjectType> GetListFillByName(string icm_obj_type_name)
        {
            string strSQL = "";
            DataRowCollection drc = null;
            IList<M_IcmMasObjectType> infoList = new List<M_IcmMasObjectType>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT icm_obj_type_seq,icm_obj_type_code,icm_obj_type_name,record_status "
                         + " ,created_by,created_when,updated_by,updated_when "
                         + " FROM icm_mas_object_type "
                         + " WHERE record_status <> 'D' and icm_obj_type_name LIKE '%' '" + icm_obj_type_name + "'  '%'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_IcmMasObjectType info = new M_IcmMasObjectType();
                        info.icm_obj_type_seq = Convert.ToInt32(drc[i]["icm_obj_type_seq"]);
                        info.icm_obj_type_code = drc[i]["icm_obj_type_code"].ToString();
                        info.icm_obj_type_name = drc[i]["icm_obj_type_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
            return infoList;
        }
        public M_IcmMasObjectType GetListFillBySeq(Nullable<Int32> icm_obj_type_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_IcmMasObjectType info = new M_IcmMasObjectType();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@icm_obj_type_seq", icm_obj_type_seq);

                strSQL = " SELECT icm_obj_type_seq,icm_obj_type_code,icm_obj_type_name,record_status "
                         +" ,created_by,created_when,updated_by,updated_when "
                         +" FROM icm_mas_object_type "
                         + " WHERE record_status <> 'D' and icm_obj_type_seq = @icm_obj_type_seq ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.icm_obj_type_seq = Convert.ToInt32(drc[0]["icm_obj_type_seq"]);
                    info.icm_obj_type_code = drc[0]["icm_obj_type_code"].ToString();
                    info.icm_obj_type_name = drc[0]["icm_obj_type_name"].ToString();
                    info.record_status = drc[0]["record_status"].ToString();
                    info.created_by = Convert.ToInt32(drc[0]["created_by"]);
                    info.created_when = Convert.ToDateTime(drc[0]["created_when"].ToString());
                    info.updated_by = cn.ConvertNullInt(drc[0]["updated_by"]);
                    info.updated_when = cn.ConvertNullDateTime(drc[0]["updated_when"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert_Icm_Object_Type(M_IcmMasObjectType info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);
                dbManager.AddParameters(0, "@icm_obj_type_code", info.icm_obj_type_code);
                dbManager.AddParameters(1, "@icm_obj_type_name", info.icm_obj_type_name);
                dbManager.AddParameters(2, "@record_status", info.record_status);
                dbManager.AddParameters(3, "@created_by", info.created_by);
                dbManager.AddParameters(4, "@created_when", info.created_when);

                strSQL = " INSERT INTO icm_mas_object_type "
                        + " (icm_obj_type_code,icm_obj_type_name,record_status "
                        + " ,created_by,created_when) "
                        + " VALUES "
                        + " (@icm_obj_type_code,@icm_obj_type_name,@record_status "
                        + " ,@created_by,@created_when) ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update_Icm_Object_Type(M_IcmMasObjectType info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);
                dbManager.AddParameters(0, "@icm_obj_type_seq", info.icm_obj_type_seq);
                dbManager.AddParameters(1, "@icm_obj_type_code", info.icm_obj_type_code);
                dbManager.AddParameters(2, "@icm_obj_type_name", info.icm_obj_type_name);
                dbManager.AddParameters(3, "@record_status", info.record_status);
                dbManager.AddParameters(4, "@updated_by", info.updated_by);
                dbManager.AddParameters(5, "@updated_when", info.updated_when);

                strSQL = " UPDATE icm_mas_object_type "
                        + " SET icm_obj_type_code = @icm_obj_type_code "
                        + " ,icm_obj_type_name = @icm_obj_type_name "
                        + " ,record_status = @record_status "
                        + " ,updated_by =@updated_by "
                        + " ,updated_when = @updated_when "
                        + " WHERE icm_obj_type_seq = @icm_obj_type_seq ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

    }
}
